package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;
    /**
     * 添加商品到购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
           //判断添加的商品是否在购物车中 动态sql
           ShoppingCart shoppingCart = new ShoppingCart();
           BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
           shoppingCart.setUserId(BaseContext.getCurrentId());
           List<ShoppingCart> list= shoppingCartMapper.list(shoppingCart);
           //是则直接在对应商品项加1
           if(list.size()>0&&list!=null){
           list.get(0).setNumber(list.get(0).getNumber()+1);
           //这里用更新
           shoppingCartMapper.updateNumberById(list.get(0));
           return;
           }
           //不是则直接插入数据
             //判断是菜品还是套餐
           if(shoppingCart.getDishId()!=null){
              //从dish表读
               Dish dish=dishMapper.getById(shoppingCart.getDishId());
               shoppingCart.setImage(dish.getImage());
               shoppingCart.setName(dish.getName());
               shoppingCart.setAmount(dish.getPrice());
           }
           else{
               SetmealVO setmealVO=setmealMapper.selectById(shoppingCart.getSetmealId());
               shoppingCart.setImage(setmealVO.getImage());
               shoppingCart.setName(setmealVO.getName());
               shoppingCart.setAmount(setmealVO.getPrice());
           }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
            //先获取当前用户的id
            Long userId=BaseContext.getCurrentId();
            ShoppingCart shoppingCart=new ShoppingCart();
            shoppingCart.setUserId(userId);
            List<ShoppingCart> list=shoppingCartMapper.list(shoppingCart);
            return list;
    }

    /**
     * 删除单个商品 数量减一
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
           ShoppingCart shoppingCart=new ShoppingCart();
           BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
           shoppingCart.setUserId(BaseContext.getCurrentId());
           List<ShoppingCart> list=shoppingCartMapper.list(shoppingCart);
           if(list.size()>0&&list!=null){
               if(list.get(0).getNumber()-1==0){
                  shoppingCartMapper.deleteById(list.get(0).getId());
               }
               list.get(0).setNumber(list.get(0).getNumber()-1);
           }
           //是更新操作 数量减一
           shoppingCartMapper.updateNumberById(list.get(0));

    }

    /**
     * 清空商品
     */
    @Override
    public void clean() {
            Long userId=BaseContext.getCurrentId();
            shoppingCartMapper.deleteByUserId(userId);
    }


}
